package com.mytest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HomeController {


    public static String name = "mike";
    public static boolean flag = false;

    @RequestMapping("/method1")
    @ResponseBody
    public String method1(){

        System.out.println(Thread.currentThread());
        System.out.println("waiting data...");
        while (name.equals("mike")){
        }
        System.out.println("=====================success");
        /*while (name.equals("mike")){
            if (flag==true){
                break;
            }
        }*/

        /*if (name.equals("jack")){
            System.out.println("=====================success");

        }

        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (name.equals("jack")){
            System.out.println("=====================success");

        }*/
        /*System.out.println(name);
        name = "tom";
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        return "ok";
    }

    @RequestMapping("/method2")
    @ResponseBody
    public String method2(){

        System.out.println(Thread.currentThread());
        /*System.out.println(name);
        name = "jack";
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        System.out.println("prepare...");
        //flag = true;
        name = "jack";
        System.out.println("prepare data end");
        return "ok";
    }

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){

        return "hello";
    }

    @RequestMapping(value = "/hello1/apply/applyInfo.do",method = RequestMethod.POST)
    @ResponseBody
    public String method1(@RequestBody String string){

        System.out.println(string);
        return "home";
    }

    @RequestMapping(value = "/aais/apply/applyInfo.",method = RequestMethod.POST)
    @ResponseBody
    public String method4(@RequestBody String string){

        System.out.println(string);
        return "{\"key\":\"home\"}";
    }

    @RequestMapping(value = "/hello2",method = RequestMethod.POST)
    @ResponseBody
    public String method2(HttpServletRequest request) throws IOException {



        InputStream inputStream = request.getInputStream();
        int i = -1;
        List<Byte> list = new ArrayList<>();
        while ((i=inputStream.read())!=-1){
            list.add((byte) i);
        }
        Object[] objects = list.toArray();
        System.out.println(objects.length);
        byte[] bytes = new byte[objects.length];
        for (int j=0;j<objects.length;j++){
            bytes[j] = (byte) objects[j];
        }


        String string = new String(bytes);
        System.out.println(string);
        return "home";
    }

    @RequestMapping(value = "/hello3",method = RequestMethod.POST)
    @ResponseBody
    public String method3(@RequestBody String string){

        System.out.println(string);
        return "home";
    }


}
